<?php


namespace addon\fenxiao\api\controller;

use addon\fenxiao\model\Config as ConfigModel;
use addon\fenxiao\model\Fenxiao as FenxiaoModel;
use addon\fenxiao\model\FenxiaoGoodsCollect as FenxiaoGoodsCollectModel;
use addon\fenxiao\model\FenxiaoGoodsSku as FenxiaoGoodsSkuModel;
use app\api\controller\BaseApi;

/**
 * 分销商品
 */
class Goods extends BaseApi
{

    /**
     * 分销商品详情
     * @return false|string
     */
    public function detail()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $sku_id = isset($this->params['sku_id']) ? $this->params['sku_id'] : 0;
        if (empty($sku_id)) {
            return $this->response($this->error('', 'REQUEST_SKU_ID'));
        }

        $fenxiao_model = new FenxiaoModel();
        $fenxiao_info  = $fenxiao_model->getFenxiaoInfo([['member_id', '=', $this->member_id]], "fenxiao_id,level_id");
        $fenxiao_info  = $fenxiao_info['data'];

        $fenxiao_goods_sku_model = new FenxiaoGoodsSkuModel();

        $condition = [['fgs.sku_id', '=', $sku_id], ['fgs.level_id', '=', $fenxiao_info['level_id']],['g.is_fenxiao','=',1]];
        $res       = $fenxiao_goods_sku_model->getFenxiaoGoodsSkuDetail($condition);

        $config = new ConfigModel();
//		$basics_config = $config->getFenxiaoBasicsConfig($this->site_id);
//		$basics_config = $basics_config['data']['value'];

        $words_config = $config->getFenxiaoWordsConfig($this->site_id);
        $words_config = $words_config['data']['value'];

        if ($res['data']) {
            $discount_price = $res['data']['discount_price'];

            // 一级佣金比例/金额
            $money = $res['data']['one_money'];
            if ($res['data']['one_rate'] > 0) {
                $money = number_format($discount_price * $res['data']['one_rate'] / 100, 2, '.', '');
            }
            $res['data']['commission_money'] = $money;
            $res['data']['words_account']    = $words_config['account'];

        }
        return $this->response($res);
    }

    /**
     * 分销商品分页列表
     */
    public function page()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $page             = isset($this->params['page']) ? $this->params['page'] : 1;
        $page_size        = isset($this->params['page_size']) ? $this->params['page_size'] : PAGE_LIST_ROWS;
        $keyword          = isset($this->params['keyword']) ? $this->params['keyword'] : '';//关键词
        $category_id      = isset($this->params['category_id']) ? $this->params['category_id'] : 0;//分类
        $min_price        = isset($this->params['min_price']) ? $this->params['min_price'] : 0;//价格区间，小
        $max_price        = isset($this->params['max_price']) ? $this->params['max_price'] : 0;//价格区间，大
        $is_free_shipping = isset($this->params['is_free_shipping']) ? $this->params['is_free_shipping'] : 0;//是否免邮
        $order            = isset($this->params['order']) ? $this->params['order'] : "create_time";//排序（综合、销量、价格）
        $sort             = isset($this->params['sort']) ? $this->params['sort'] : "desc";//升序、降序
        $goods_id_arr     = isset($this->params['goods_id_arr']) ? $this->params['goods_id_arr'] : '';//goods_id数组

        $condition = [
            ['g.is_fenxiao', '=', 1],
            ['gs.goods_state', '=', 1],
            ['gs.is_delete', '=', 0]
        ];

        if (!empty($keyword)) {
            $condition[] = ['gs.sku_name|gs.keywords', 'like', '%' . $keyword . '%'];
        }

        if (!empty($category_id)) {
            $condition[] = ['g.category_id', 'like', [$category_id, '%' . $category_id . ',%', '%' . $category_id, '%,' . $category_id . ',%'], 'or'];
        }

        if (!empty($goods_id_arr)) {
            $condition[] = ['gs.goods_id', 'in', $goods_id_arr];
        }

        if ($min_price != "" && $max_price != "") {
            $condition[] = ['gs.discount_price', 'between', [$min_price, $max_price]];
        } elseif ($min_price != "") {
            $condition[] = ['gs.discount_price', '>=', $min_price];
        } elseif ($max_price != "") {
            $condition[] = ['gs.discount_price', '<=', $max_price];
        }

        if (!empty($is_free_shipping)) {
            $condition[] = ['gs.is_free_shipping', '=', $is_free_shipping];
        }

        // 非法参数进行过滤
        if ($sort != "desc" && $sort != "asc") {
            $sort = "";
        }

        // 非法参数进行过滤
        if ($order != '') {
            if ($order != "sale_num" && $order != "discount_price") {
                $order = 'gs.create_time';
            } else {
                $order = 'gs.' . $order;
            }
            $order_by = $order . ' ' . $sort;
        } else {
            $order_by = 'fgs.goods_sku_id desc,gs.sort desc,gs.create_time desc';
        }

        if (!empty($keyword)) {
            $condition[] = ['gs.sku_name', 'like', '%' . $keyword . '%'];
        }

        $fenxiao_model = new FenxiaoModel();
        $fenxiao_info  = $fenxiao_model->getFenxiaoInfo([['member_id', '=', $this->member_id]], "fenxiao_id,level_id");
        $fenxiao_info  = $fenxiao_info['data'];

        // 获取当前用户的分销等级
        $condition[] = ['fgs.level_id', '=', $fenxiao_info['level_id']];

//		$config = new ConfigModel();
//		$basics_config = $config->getFenxiaoBasicsConfig($this->site_id);
//		$basics_config = $basics_config['data']['value'];

        $fenxiao_goods_sku_model = new FenxiaoGoodsSkuModel();
        $list = $fenxiao_goods_sku_model->getFenxiaoGoodsSkuPageList($condition, $page, $page_size, $order_by);

        $fenxiao_goods_collect_model = new FenxiaoGoodsCollectModel();

        // 计算佣金比率
        foreach ($list['data']['list'] as $k => $v) {

            $collection_info = $fenxiao_goods_collect_model->getCollectInfo([
                ['member_id', '=', $this->member_id],
                ['sku_id', '=', $v['sku_id']],
                ['fenxiao_id', '=', $fenxiao_info['fenxiao_id']],
            ], 'collect_id');

            // 查询是否关注该分销商品
            $collection_info = $collection_info['data'];
            if (!empty($collection_info)) {
                $list['data']['list'][$k]['is_collect'] = 1;
                $list['data']['list'][$k]['collect_id'] = $collection_info['collect_id'];
            } else {
                $list['data']['list'][$k]['is_collect'] = 0;
            }

            $discount_price = $v['discount_price'];

            // 一级佣金比例/金额
            $money = $v['one_money'];
            if ($v['one_rate'] > 0) {
                $money = number_format($discount_price * $v['one_rate'] / 100, 2, '.', '');
            }

            $list['data']['list'][$k]['commission_money'] = $money;
        }

        return $this->response($list);

    }

}